package com.automannn.practice.mybatis.mybatisPlusSpring;


import com.automannn.practice.mybatis.dao.PersonDao;
import com.automannn.practice.mybatis.dao.PersonPlusTestDao;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Properties;

/**
 * @author automannn
 * @Date 2022/8/17
 */
@ComponentScan
public class MybatisPlusSpringTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MybatisPlusSpringTest.class);


        //第一种，直接通过sql查询
        SqlSession sqlSession= (SqlSession) applicationContext.getBean("sqlSessionTestBySql");
        List list =  sqlSession.selectList("com.automannn.practice.mybatis.dao.PersonDao.selectList");
        System.out.println(list);

        System.out.println("========通过接口查询============");
        //第二种，获取接口查询
        System.out.println(applicationContext.getBean(PersonDao.class));
        PersonDao personDao= sqlSession.getMapper(PersonDao.class);
        System.out.println(personDao.selectList());

        System.out.println(applicationContext.getBean(PersonPlusTestDao.class));
        PersonPlusTestDao personPlusTestDao= sqlSession.getMapper(PersonPlusTestDao.class);
        System.out.println(personPlusTestDao.selectPage(new Page<>(),Wrappers.emptyWrapper()));
        System.out.println("mybatis原生环境下，就算没有写Mapper也不会报错");

    }

    @Component
    public static class MybatisPlusConfig{
        @Bean
        /*xxx: spring通过工厂类实现的自动注入: MapperFactoryBean,最终依然是通过 sqlSession获取的对象*/
        public MapperScannerConfigurer mybatisScanner(){
            MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
            mapperScannerConfigurer.setBasePackage("com.automannn.practice.mybatis.dao");
            return mapperScannerConfigurer;
        }

        @Bean
        public SqlSessionTemplate sqlSessionTestBySql(SqlSessionFactory sqlSessionFactory){
            try {
                return new SqlSessionTemplate(sqlSessionFactory);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }


        @Bean
        public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(){

            //xxx: 与 mybatis不同的是  改写了 factory: com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean
            //xxx: 此外，它与 mybatis 的factoryBean并不存在父子关系
            MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();

            PooledDataSource dataSource = new PooledDataSource("com.mysql.cj.jdbc.Driver","jdbc:mysql://localhost:3306/zhenhe?useUnicode=true;serverTimezone=CST;characterEncoding=utf-8","root","chenkaihai");

            Properties properties = new Properties();
            properties.put("useUnicode","true");
            properties.put("serverTimezone","CST");
            properties.put("characterEncoding","utf-8");
            properties.put("dbname","zhenhe");

            dataSource.setDriverProperties(properties);
            sqlSessionFactoryBean.setDataSource(dataSource);

            sqlSessionFactoryBean.setPlugins(new PaginationInterceptor());

            Resource resource = new ClassPathResource("org/mybatis/example/mapper/PersonMapper.xml");
            sqlSessionFactoryBean.setMapperLocations(resource);


            return sqlSessionFactoryBean;
        }




    }
}
